import config
import webapi
import util
import pandas as pd


def get_commitcomment_data(commitcomment_url):
    api_data = webapi.get_commmitcomment_data(commitcomment_url)
    data_df = util.clean_commitcomment_data(api_data)
    return data_df


def main(config):
    for _ in config['repository_list']:
        comment_df = pd.read_csv(config['data_path']['commit'].format(_.split('/')[-1])+'.csv')
        sha_list = []
        for index, row in comment_df.iterrows():
            if row["comment_count"]:
                sha_list.append(row["sha"])
        for index, sha in enumerate(sha_list):
            commitcomment_url = config['base_url'] + '/repos{}/commits/{}/comments'.format(_, sha)
            #判断是否只有一个commitcomment
            if index:
                data.append(get_commitcomment_data(commitcomment_url))
            else:
                data = get_commitcomment_data(commitcomment_url)
        data.to_csv(config['data_path']['commitcomments'].format(_.split('/')[-1]) + '.csv')
        # data.to_excel(config['data_path']['issuecomments'].format(_.split('/')[-1]) + '.xlsx')


if __name__ == '__main__':
    _config = config.get_config()
    main(_config)
